package com.sky.task;


import com.sky.entity.Orders;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.sky.mapper.OrderMapper;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Component
@Slf4j
public class OrderTask {
    @Autowired
    private OrderMapper orderMapper;
//    @Scheduled(cron = "0 * * * * ?")
//@Scheduled(cron = "0/5 * * * * ?")
    public void processTimeoutOrder(){
        log.info("定时处理超时订单,{}", LocalDateTime.now());
        LocalDateTime beforeTime15=LocalDateTime.now().plusMinutes(-15);
        List<Orders> ordersList=orderMapper.getBystatusAndOrderTime(Orders.PENDING_PAYMENT,beforeTime15);
         for(Orders orders:ordersList){
             System.out.println(orders.getAmount()+"元的订单超时未支付，自动取消");
             orders.setStatus(orders.CANCELLED);
             orders.setCancelTime(LocalDateTime.now());
             orderMapper.update(orders);
         }
    }
//@Scheduled(cron = "0 0 1 * * ?")
//@Scheduled(cron = "0/5 * * * * ?")
    public void processDeliverOrder(){
        log.info("处理派送中的订单,{}", LocalDateTime.now());
        LocalDateTime beforeTime60=LocalDateTime.now().plusMinutes(-60);
        List<Orders> ordersList=orderMapper.getBystatusAndOrderTime(Orders.DELIVERY_IN_PROGRESS,beforeTime60);
        for(Orders orders:ordersList){
            orders.setStatus(orders.COMPLETED);
            orderMapper.update(orders);
        }
    }
}
